﻿Public Partial Class Loading_Popup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Dim lbUser As Label = CType(Master.FindControl("lbMenu"), Label)
            'lbUser.Text = "Loading/Unloading Popup"
            If (Session("EditMaster") Is Nothing) Then
                btnSave.Visible = False
            End If
            ViewState("Facility") = CreateFacility()
            ViewState("Loading") = ""
            Dim ds As DataTable = clsGet.Get_Shore("", "L")
            If (ds.Rows.Count > 0) Then
                With ddlShore
                    .ClearSelection()
                    .DataTextField = "Shore_Name"
                    .DataValueField = "Shore_Code"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--กรุณาเลือก-->")
                    .Items(0).Value = "0"
                End With
            End If
            ds = clsGet.Get_ProductGroupMaster("", "L")
            If (ds.Rows.Count > 0) Then
                With ddlProduct
                    .ClearSelection()
                    .DataTextField = "Group_name"
                    .DataValueField = "Group_no"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--กรุณาเลือก-->")
                    .Items(0).Value = "0"
                End With
            End If
            ddlWharf.Items.Clear()
            ddlWharf.Items.Insert(0, "<--กรุณาเลือก-->")
            ddlWharf.Items(0).Value = "0"
            ds = clsGet.Get_WharfAll("L")
            If (ds.Rows.Count > 0) Then
                With ddlWharf
                    .Items.Clear()
                    .ClearSelection()
                    .DataTextField = "Wharf"
                    .DataValueField = "BookingWharf"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--กรุณาเลือก-->")
                    .Items(0).Value = "0"
                End With
            End If

            ddlLine.Items.Clear()
            ddlLine.Items.Insert(0, "<--กรุณาเลือก-->")
            ddlLine.Items(0).Value = "0"
            If Not (Session("Loading") Is Nothing) Then
                ViewState("Loading") = Session("Loading")
                Dim dt As DataTable = clsGet.Get_LoadingUnloadingByOne(Session("Loading"))
                If (dt.Rows.Count = 1) Then
                    If Not (ddlShore.Items.FindByValue(dt.Rows(0).Item("Shore_Code")) Is Nothing) Then
                        ddlShore.SelectedValue = dt.Rows(0).Item("Shore_Code")
                    End If
                    If Not (ddlProduct.Items.FindByValue(dt.Rows(0).Item("Group_Code")) Is Nothing) Then
                        ddlProduct.SelectedValue = dt.Rows(0).Item("Group_Code")
                    End If
                    ds = clsGet.Get_Line(ddlShore.SelectedValue, "")
                    If (ds.Rows.Count > 0) Then
                        With ddlLine
                            .Items.Clear()
                            .ClearSelection()
                            .DataTextField = "Line_Name"
                            .DataValueField = "Line_Code"
                            .DataSource = ds
                            .DataBind()
                            .Items.Insert(0, "<--กรุณาเลือก-->")
                            .Items(0).Value = "0"
                        End With
                    End If
                    If Not (ddlLine.Items.FindByValue(dt.Rows(0).Item("Line_Code")) Is Nothing) Then
                        ddlLine.SelectedValue = dt.Rows(0).Item("Line_Code")
                    End If
                    

                    dt = clsGet.Get_Loding_Facility(ViewState("Loading"))
                    Dim dn As DataTable = ViewState("Facility")
                    For Each dr As DataRow In dt.Rows
                        Dim de As DataRow = dn.NewRow()
                        de.Item("Facility_Code") = dr.Item("Facility_Code")
                        de.Item("Facility") = dr.Item("Facility")
                        de.Item("BookingWharf") = dr.Item("BookingWharf")
                        de.Item("Wharf") = dr.Item("Wharf")
                        de.Item("L_D") = dr.Item("L_D")
                        de.Item("PORT") = dr.Item("PORT")
                        de.Item("Before") = dr.Item("Before")
                        de.Item("After") = dr.Item("After")
                        de.Item("Rate") = dr.Item("Rate")
                        de.Item("Unit") = dr.Item("Unit")
                        de.Item("Remark") = dr.Item("Remark")
                        If (txtTank.Text = "0") Then
                            de.Item("RateText") = dr.Item("Before")
                        Else
                            de.Item("RateText") = dr.Item("Before") & "+ (" & dr.Item("Plus_Tank") & "T)"
                        End If
                        de.Item("Plus_Tank") = dr.Item("Plus_Tank")
                        de.Item("Plus_Parcel_Before") = dr.Item("Plus_Parcel_Before")
                        de.Item("Plus_Product_Before") = dr.Item("Plus_Product_Before")
                        de.Item("Plus_Parcel_After") = dr.Item("Plus_Parcel_After")
                        de.Item("Plus_Product_After") = dr.Item("Plus_Product_After")
                        dn.Rows.Add(de)
                        dn.AcceptChanges()
                    Next
                    ViewState("Facility") = dn
                    gridFacility.DataSource = dn
                    gridFacility.DataBind()


                End If
            End If
        End If
    End Sub

    Private Function CreateFacility()
        Dim dt As New DataTable
        dt.Columns.Add("Facility_Code", GetType(String))
        dt.Columns.Add("Facility", GetType(String))
        dt.Columns.Add("BookingWharf", GetType(String))
        dt.Columns.Add("Wharf", GetType(String))
        dt.Columns.Add("PORT", GetType(String))
        dt.Columns.Add("L_D", GetType(String))
        dt.Columns.Add("Before", GetType(String))
        dt.Columns.Add("After", GetType(String))
        dt.Columns.Add("Rate", GetType(String))
        dt.Columns.Add("Unit", GetType(String))
        dt.Columns.Add("Remark", GetType(String))
        dt.Columns.Add("RateText", GetType(String))
        dt.Columns.Add("Plus_Tank", GetType(String))
        dt.Columns.Add("Plus_Parcel_Before", GetType(String))
        dt.Columns.Add("Plus_Product_Before", GetType(String))
        dt.Columns.Add("Plus_Parcel_After", GetType(String))
        dt.Columns.Add("Plus_Product_After", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function


    Private Sub ddlShore_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlShore.SelectedIndexChanged
        Dim ds As DataTable = clsGet.Get_Line(ddlShore.SelectedValue, "")
        If (ds.Rows.Count > 0) Then
            With ddlLine
                .Items.Clear()
                .ClearSelection()
                .DataTextField = "Line_Name"
                .DataValueField = "Line_Code"
                .DataSource = ds
                .DataBind()
                .Items.Insert(0, "<--กรุณาเลือก-->")
                .Items(0).Value = "0"
            End With
        End If
    End Sub

    Private Sub gridFacility_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridFacility.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim dt As DataTable = ViewState("Facility")
        If (e.CommandName = "cmdedit") Then
            If Not (ddlLD.Items.FindByValue(dt.Rows(Index).Item("L_D")) Is Nothing) Then
                ddlLD.SelectedValue = dt.Rows(Index).Item("L_D") & ""
            End If
            ViewState("EventClick") = Index
            txtFacility.Text = dt.Rows(Index).Item("Facility")
            txtBefore.Text = dt.Rows(Index).Item("Before")
            txtRate.Text = dt.Rows(Index).Item("Rate")
            txtAfter.Text = dt.Rows(Index).Item("After")
            txtTank.Text = dt.Rows(Index).Item("Plus_Tank")
            txtParcel.Text = dt.Rows(Index).Item("Plus_Parcel_Before")
            txtProduct.Text = dt.Rows(Index).Item("Plus_Product_Before")
            txtParcelAfter.Text = dt.Rows(Index).Item("Plus_Parcel_After")
            txtProductAfter.Text = dt.Rows(Index).Item("Plus_Product_After")
            If Not (ddlUnit.Items.FindByValue(dt.Rows(Index).Item("Unit")) Is Nothing) Then
                ddlUnit.SelectedValue = dt.Rows(Index).Item("Unit") & ""
            End If
            txtRemark.Text = dt.Rows(Index).Item("Remark")
            Dim ds As DataTable = clsGet.Get_WharfAll(dt.Rows(Index).Item("PORT"))
            If (ds.Rows.Count > 0) Then
                With ddlWharf
                    .Items.Clear()
                    .ClearSelection()
                    .DataTextField = "Wharf"
                    .DataValueField = "BookingWharf"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--กรุณาเลือก-->")
                    .Items(0).Value = "0"
                End With
            End If
            If Not (ddlWharf.Items.FindByValue(dt.Rows(Index).Item("BookingWharf")) Is Nothing) Then
                ddlWharf.SelectedValue = dt.Rows(Index).Item("BookingWharf") & ""
            End If
        ElseIf (e.CommandName = "cmddelete") Then

            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            ViewState("Facility") = dt
            gridFacility.DataSource = dt
            gridFacility.DataBind()
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Session("Loading") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (ddlShore.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุุณาทำการเลือก Shore !!');", True)
            Exit Sub
        End If
        If (ddlProduct.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุุณาทำการเลือก Product !!');", True)
            Exit Sub
        End If
        If (ddlLine.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุุณาทำการเลือก Line !!');", True)
            Exit Sub
        End If
        Dim DtFacility As DataTable = ViewState("Facility")
        If (DtFacility.Rows.Count = 0) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุุณาทำการเลือก Facility !!');", True)
            Exit Sub
        End If

        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        Dim Load_Code As String = ""
        If ViewState("Loading").ToString() = "" Then
            Load_Code = clsDB.GeneratorID(conn, "Loading_Unloading")
        Else
            Load_Code = ViewState("Loading").ToString()
        End If

        tran = clsDB.BeginTran(conn)
        If (clsFc.InsertLoading_Unloading(ViewState("Loading").ToString(), Load_Code, ddlShore.SelectedValue, ddlProduct.SelectedValue, ddlLine.SelectedValue, conn, tran) > 0) Then
            clsFc.DeleteLoading_Facility(Load_Code, conn, tran)
            For Each dr As DataRow In DtFacility.Rows
                If (clsFc.InsertLoading_Facility(Load_Code, dr.Item("L_D"), dr.Item("BookingWharf"), dr.Item("Facility_Code"), dr.Item("Before"), dr.Item("Rate"), dr.Item("Unit"), dr.Item("After"), dr.Item("Remark"), dr.Item("Plus_Tank"), dr.Item("Plus_Parcel_Before"), dr.Item("Plus_Product_Before"), dr.Item("Plus_Parcel_After"), dr.Item("Plus_Product_After"), conn, tran) < 1) Then
                    condition = False
                End If
            Next
        Else
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "parent.closeModal();alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If (ddlWharf.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการเลือก Wharf');", True)
            Exit Sub
        End If
        If (txtFacility.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Facility');", True)
            Exit Sub
        End If
        If (txtBefore.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Before');", True)
            Exit Sub
        End If
        If (txtRate.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Rate');", True)
            Exit Sub
        End If
        If (txtAfter.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ After');", True)
            Exit Sub
        End If
        Dim Facility_Code As String = clsGet.Get_FacilityCode(txtFacility.Text)
        If (Facility_Code = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบรายการ Facility ที่ระบุ');", True)
            Exit Sub
        End If
        Dim dt As DataTable = ViewState("Facility")
        If (ViewState("EventClick") Is Nothing) Then
            Dim dr As DataRow = dt.NewRow()
            dr.Item("Facility_Code") = Facility_Code
            dr.Item("Facility") = txtFacility.Text
            dr.Item("BookingWharf") = ddlWharf.SelectedValue
            dr.Item("Wharf") = ddlWharf.SelectedItem.Text
            dr.Item("L_D") = ddlLD.SelectedValue
            dr.Item("PORT") = ""
            dr.Item("Before") = txtBefore.Text
            dr.Item("After") = txtAfter.Text
            dr.Item("Rate") = txtRate.Text
            dr.Item("Unit") = ddlUnit.SelectedValue
            dr.Item("Remark") = txtRemark.Text
            If (txtTank.Text = "0") Then
                dr.Item("RateText") = txtBefore.Text
            Else
                dr.Item("RateText") = txtBefore.Text & "+ (" & txtTank.Text & "T)"
            End If
            dr.Item("Plus_Tank") = txtTank.Text
            dr.Item("Plus_Parcel_Before") = txtParcel.Text
            dr.Item("Plus_Product_Before") = txtProduct.Text
            dr.Item("Plus_Parcel_After") = txtParcelAfter.Text
            dr.Item("Plus_Product_After") = txtProductAfter.Text
            dt.Rows.Add(dr)
            dt.AcceptChanges()
            ViewState("Facility") = dt
            gridFacility.DataSource = dt
            gridFacility.DataBind()
        Else
            Dim Index As Integer = CInt(ViewState("EventClick"))
            dt.Rows(Index).Item("Facility_Code") = Facility_Code
            dt.Rows(Index).Item("Facility") = txtFacility.Text
            dt.Rows(Index).Item("BookingWharf") = ddlWharf.SelectedValue
            dt.Rows(Index).Item("Wharf") = ddlWharf.SelectedItem.Text
            dt.Rows(Index).Item("L_D") = ddlLD.SelectedValue
            dt.Rows(Index).Item("PORT") = ""
            dt.Rows(Index).Item("Before") = txtBefore.Text
            dt.Rows(Index).Item("After") = txtAfter.Text
            dt.Rows(Index).Item("Rate") = txtRate.Text
            dt.Rows(Index).Item("Unit") = ddlUnit.SelectedValue
            dt.Rows(Index).Item("Remark") = txtRemark.Text
            If (txtTank.Text = "0") Then
                dt.Rows(Index).Item("RateText") = txtBefore.Text
            Else
                dt.Rows(Index).Item("RateText") = txtBefore.Text & "+ (" & txtTank.Text & "T)"
            End If

            dt.Rows(Index).Item("Plus_Tank") = txtTank.Text
            dt.Rows(Index).Item("Plus_Parcel_Before") = txtParcel.Text
            dt.Rows(Index).Item("Plus_Product_Before") = txtProduct.Text
            dt.Rows(Index).Item("Plus_Parcel_After") = txtParcelAfter.Text
            dt.Rows(Index).Item("Plus_Product_After") = txtProductAfter.Text
            dt.AcceptChanges()
            ViewState("Facility") = dt
            gridFacility.DataSource = dt
            gridFacility.DataBind()
        End If
        ClearText()
    End Sub

    Private Sub ClearText()
        txtFacility.Text = String.Empty
        ddlWharf.ClearSelection()
        ddlLD.ClearSelection()
        txtBefore.Text = String.Empty
        txtRate.Text = "0"
        ddlUnit.ClearSelection()
        txtAfter.Text = String.Empty
        txtRemark.Text = String.Empty
        txtTank.Text = "0"
        txtParcel.Text = "00:00"
        txtProduct.Text = "00:00"
        txtParcelAfter.Text = "00:00"
        txtProductAfter.Text = "00:00"
        ViewState("EventClick") = Nothing
    End Sub

    Private Sub gridFacility_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridFacility.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (gridFacility.DataKeys(e.Row.RowIndex).Values("L_D") & "" = "L") Then
                e.Row.Cells(0).Text = " Load"
            ElseIf (gridFacility.DataKeys(e.Row.RowIndex).Values("L_D") & "" = "D") Then
                e.Row.Cells(0).Text = "Disch"
            End If
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ClearText()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class